﻿//-----------------------------------------------------------------------
// <copyright file="SlwyEventBus.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: SlwyEventBus.cs
// * history : created by qinchaoyue 2018-03-02 05:25:08
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Slwy.Data.Entity.Interfaces;
using Slwy.EventBus.Interfaces;
using Slwy.EventBus.Models;
using Slwy.FrameCore.Helper;
using Slwy.FrameCore.Extension;
using Slwy.KeyIDMaker;
using Slwy.Logger.Contracts;
using Slwy.Utility.Extensions;

namespace Slwy.EventBus.Contracts
{
    /// <summary>
    /// 事件总线 helper
    /// </summary>
    public class SlwyEventBus : ISlwyEventBus
    {
        public ISlwyHttpClient HttpClient {protected get; set; }

        public ILogger Logger { protected get; set; }

        public IEventInfoRepository EventInfoRepo { get; set; }

        public IEventListen EventListen { get; set; }

        /// <summary>
        /// 发送事件
        /// </summary>
        /// <param name="routingKey"> 路由 key </param>
        /// <param name="data"> 数据 </param>
        /// <param name="delayTime"> 延迟毫秒数 </param>
        /// <param name="errMessage"> 发送失败返回消息 </param>
        /// <returns>成功或者失败</returns>
        public bool SendEvent(string routingKey, object data, out string errMessage, long delayTime = 0)
        {
            errMessage = string.Empty;
            try
            {
                var result = HttpClient.Post<MSendEventResult>("/api/eventbus/event", new
                {
                    routingKey,
                    data = data.ToJson(),
                    delayTime,
                    callBackType = "dynamic"
                });
                if (result.BusinessCode != 1)
                {
                    errMessage = result.Message;
                    return false;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                errMessage = e.Message;
                return false;
            }

            return false;
        }

        public bool SendEventTrans<T>(IBaseRepository<T> repo, string routingKey, object data, out string errMessage, long delayTime = 0) where T : class, new()
        {
            errMessage = string.Empty;
            var entity = new EventInfoEntity
            {
                KeyID = KeyIDMaker.KeyIDMakerHalder.NewKeyID(BizType.VipHall),
                CallBackType = "dynamic",
                RoutingKey = routingKey,
                Data = data.ToJson(),
                DelayTime = delayTime,
                ValiDateTime = DateTime.Now.AddDays(1),
                ConnectionString = repo.Client.Ado.Connection.ConnectionString,
            };

            var result = EventInfoRepo.InsertEventInfo(repo, entity);
            repo.OnSaveChanged += EventListen.OnSaveChanged;
            EventListen.PendEventInfos.TryAdd(repo.Client.ContextID.ToString(), entity);

            return result;
        }

        //public bool SendEventTrans(string routingKey, object data, out string errMessage, long delayTime = 0)
        //{
            
        //}
    }
}
